package com.docios.cookbook2.ui.homePager;

import android.content.Intent;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.docios.cookbook2.Dao.AppDatabase;
import com.docios.cookbook2.Dao.DatabaseImpl;
import com.docios.cookbook2.Dao.UserDao;
import com.docios.cookbook2.databinding.FragmentUserBinding;
import com.docios.cookbook2.entity.User;
import com.docios.cookbook2.ui.loginPager.LoginActivity;

import java.util.List;

public class UserFragment extends Fragment {
    FragmentUserBinding userBinding;
    UserDao userDao;
    AppDatabase db;
    List<User> users;

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        userBinding=FragmentUserBinding.inflate(getLayoutInflater());

        initData();
        initView();
        userBinding.btnVip.setOnClickListener(v -> {
            Toast.makeText(getActivity(), "阿Sir，你来真的啊", Toast.LENGTH_SHORT).show();
        });
        userBinding.btnDelete.setOnClickListener(v -> {
            userDao.deleteU(users.get(0));
            Intent intent=new Intent(getActivity(), LoginActivity.class);
            startActivity(intent);
            HomeActivity homeActivity = (HomeActivity) getActivity();
            assert homeActivity != null;
            homeActivity.finishMain();
        });


        return userBinding.getRoot();
    }


    private void initData() {
        db = DatabaseImpl.setInstance(getActivity());
        userDao = db.userDao();

        //父activity传的phone
        Bundle bundle=this.getArguments();
        if (bundle!=null){
            users= userDao.getAllByPhoneU(bundle.getString("phone"));
        }
    }

    private void initView() {
        userBinding.nickName.setText(users.get(0).phone);
    }

}